Helpful Information
 
 
Category: Form validation
Form validation -> checkbox array

<form name="new_page" action="test.php"><input type="checkbox" name="allowed[]" value="2">
<input type="checkbox" name="allowed[]" value="2">
<input type="checkbox" name="allowed[]" value="2">
<input class="button_140" type="button" value="Toevoegen"
onclick="javascript:
if (!document.new_page.allowed[].checked){ popupalert('allowed'); }
else { this.form.submit(); }"></form>

This is a piece of the code I've written. It's supposed to check if one of the allowed[]-checkboxes is checked, and if not, to run function popupalert('allowed');. It's not working. Can someone tell me how to fix this?

I take it the form is only to be submitted if one of the checkboxes has been checked?



<script type="text/javascript">

function chkChecks(){
isChecked=false

for(var i=0;i<document.forms["new_page"]["allowed[]"].length;i++){
if(document.forms["new_page"]["allowed[]"][i].checked){
isChecked=true
}
}

if(isChecked){
document.forms["new_page"].submit()
}
else{
alert('Please select a checkbox')
}

}

</script>

<form name="new_page" action="test.php">
<input type="checkbox" name="allowed[]" value="2">
<input type="checkbox" name="allowed[]" value="2">
<input type="checkbox" name="allowed[]" value="2">
<input class="button_140" type="button" value="Toevoegen" onclick="chkChecks()">
</form>










privacy (GDPR)